//
//  ReportV.m
//  UHealth
//
//  Created by gjtxz on 15/1/7.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "ReportV.h"
#import "MJRefresh.h"
#import "DoctorInfoCell.h"
#import "OnlineConsultLeft1Cell.h"

#define SYSTEM_VERSION                              ([[UIDevice currentDevice] systemVersion])
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([SYSTEM_VERSION compare:v options:NSNumericSearch] != NSOrderedAscending)
#define IS_IOS8_OR_ABOVE                            (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))



@interface ReportV ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)OnlineConsultLeft1Cell     *properCell;

@end

@implementation ReportV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self DrawViewWithFrame:frame];
    }
    return self;
}

-(void)DrawViewWithFrame:(CGRect)frame
{
    _reportArr = @[@{@"headImageUrl" : @"doctor", @"name" : @"杨医生", @"hospitalName" : @"广州中山慢病管理机构"},
                   @{@"userPic" : @"doctor", @"addtime" : @"2014-12-16 16:38:06", @"content" : @"您的健康数据分析在餐前正常，您需要在控制饮食、适度运动和口服药物降糖等综合治疗过程中，需要定期进行血液、尿液、肾功能、心功能及眼底等有关项目的检查，以便随时观察和掌握病情的变化。"},
                   @{@"userPic" : @"doctor", @"addtime" : @"2014-12-20 17:50:39", @"content" : @"您最近的血糖严重超标，请您注意。\n\n控糖主食以粗杂粮,如燕麦,麦片,玉米面等为主，这些食物中有较多的无机盐、维生素、又富含膳食纤维，膳食纤维具有减低血糖作用，对控制血糖有利。\n\n蛋白质来源，以大豆及其豆制品为好。一方面，其所含蛋白质量多质好；另一方面，其不含胆固醇，具有降脂作用，故可代替部分动物性食品，如肉类等。\n\n在控制热量期间，仍感饥饿时，可食用含糖少的蔬菜，用水煮后加一些佐料拌着吃，由于蔬菜所含膳食纤维多，水分多，供热能低，具有饱腹作用，是糖尿病患者必不可少的食物。\n\n祝您健康。"},
                   @{@"userPic" : @"doctor", @"addtime" : @"2014-12-24 14:39:18", @"content" : @"您最近血糖控制的不错，请继续保持。\n\n另外，适当运动也有助于您血糖控制，一周三四次，每次1-2小时的快走，慢跑，打球等运动有助于控糖。\n\n心脏病，高血压病人则须在医生知道下选择和进行运动。"},
                   @{@"userPic" : @"doctor", @"addtime" : @"2014-12-26 18:09:22", @"content" : @"您的空腹血糖值偏低，请您留意。"}];
    
    self.dataSource = self;
    self.delegate = self;
    __unsafe_unretained typeof(self) vc = self;
    [self addHeaderWithCallback:^{
        
        // 模拟延迟加载数据，
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 结束刷新
            [vc headerEndRefreshing];
        });
    }];
    [self registerNib:[UINib nibWithNibName:NSStringFromClass([DoctorInfoCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([DoctorInfoCell class])];
    [self registerNib:[UINib nibWithNibName:NSStringFromClass([OnlineConsultLeft1Cell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([OnlineConsultLeft1Cell class])];
}

- (OnlineConsultLeft1Cell *)properCell
{
    if (!_properCell) {
        _properCell = [self dequeueReusableCellWithIdentifier:NSStringFromClass([OnlineConsultLeft1Cell class])];
    }
    return _properCell;
}



#pragma mark UItableView


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _reportArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 60;
    }
    else {
        if (IS_IOS8_OR_ABOVE) {
            return UITableViewAutomaticDimension;
        }
        else {
            return 100;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 60;
    }
    else {
        if (IS_IOS8_OR_ABOVE) {
            return UITableViewAutomaticDimension;
        }
        else {
            NSDictionary *dataDict = _reportArr[indexPath.row];
            self.properCell.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.properCell.bounds));
            [self.properCell setData:dataDict];
            [self.properCell updateConstraintsIfNeeded];
            [self.properCell layoutIfNeeded];
            
            return [self.properCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        }
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0) {
        DoctorInfoCell *infoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DoctorInfoCell class]) forIndexPath:indexPath];
        
        NSDictionary *dataDict = _reportArr[indexPath.row];
        [infoCell setData:dataDict];
        
        return infoCell;
    }
    else {
        OnlineConsultLeft1Cell *consultCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OnlineConsultLeft1Cell class]) forIndexPath:indexPath];
        
        NSDictionary *dataDict = _reportArr[indexPath.row];
        [consultCell setData:dataDict];
        [consultCell updateConstraintsIfNeeded];
        [consultCell layoutIfNeeded];
        
        return consultCell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
